package com.jinyi.wwdeng.ui.main.impl

import com.jinyi.base.mvp.BaseModel
import com.jinyi.base.network.IRequestCallback
import com.jinyi.base.network.NetworkUtils
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.network.ApiService
import com.jinyi.wwdeng.ui.main.contract.CreditLimitContract
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/28 16:19
 * @Annotation：Model层  接口实现类
 */
class CreditLimitModelImpl(iCallback: CreditLimitContract.Presenter): BaseModel<CreditLimitContract.Presenter>(iCallback),CreditLimitContract.Model {
    /**
     * 获取用户贷款额度
     */
    override fun loadCreditLimitRequest(params: JSONObject) {
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        NetworkUtils.post(httpUrl,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                callBack.onCreditLimitSuccess(data)
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                callBack.onRequestError(data.optString("errorMsg"))
            }
            //请求完成
            override fun onRequestFinish() {
                callBack.onRequestFinish()
            }
        })
    }

    /**
     * 获取所有权限列表
     */
    override fun loadPermissionList() {
        val commonData = JSONObject(AppConfigs.getSPString(AppConfigs.SP_KEY.SP_HENECREDIT_COMMON))
        val customize = commonData.optJSONObject("customize")
        val allPermissionList = arrayListOf<String>()
        if(customize != null){
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null){
                val permission = permissions.optJSONArray("permission")
                if (permission != null && permission.length() > 0){
                    for (i in 0..<permission.length()){
                        allPermissionList.add(permission.optString(i))
                    }
                }

                val home = permissions.optJSONArray("home")
                if (home != null && home.length() > 0){
                    for (i in 0..<home.length()){
                        allPermissionList.add(home.optString(i))
                    }
                }
            }
        }
        callBack.onPermissionListSuccess(allPermissionList)
    }

}